﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StudentInfo;
using System.IO;
using System.Runtime.Serialization;

namespace Ex_17._3_Input_StudentInfo
{
    public partial class InputStudentInfoForm : StudentInfoUIForm
    {
        public InputStudentInfoForm()
        {
            InitializeComponent();
        }

        private BinaryFormatter formatter = new BinaryFormatter();
        private FileStream output;

        private void saveFileButton_Click(object sender, EventArgs e)
        {
            DialogResult result;
            string fileName;

            using (SaveFileDialog fileChooser = new SaveFileDialog())
            {
                fileChooser.CheckFileExists = false;
                result = fileChooser.ShowDialog();
                fileName = fileChooser.FileName;
            }

            if (result == DialogResult.OK)
            {
                if (fileName != string.Empty)
                {
                    try
                    {
                        output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);

                        TextBoxesEnabled(true);
                        saveFileButton.Enabled = false;
                        enterButton.Enabled = true;
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void enterButton_Click(object sender, EventArgs e)
        {
            string[] values = GetTextBoxValues();
            Student student = new Student();
            bool validData = true;

            foreach (string value in values)
            {
                if (value == string.Empty)
                    validData = false;
            }

            if (validData)
            {
                try
                {
                    int studentID = Convert.ToInt32(values[(int) TextBoxIndices.ID]);

                    if (studentID >= 0)
                    {


                        student.FirstName = values[(int) TextBoxIndices.FIRSTNAME];
                        student.LastName = values[(int) TextBoxIndices.LASTNAME];
                        student.ID = studentID;
                        student.StudentClass = values[(int) TextBoxIndices.CLASS];
                        student.Grade = values[(int) TextBoxIndices.GRADE];

                        formatter.Serialize(output, student);

                        infoProcessLabel.Text = string.Format("Data for {0} {1} was entered", student.FirstName,
                                                              student.LastName);
                        ClearTextBoxes();
                    }
                    else
                    {
                        MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (SerializationException)
                {
                    MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Data Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Please fill all fields", "Incomplete entered data", MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
        }

        private void doneButton_Click(object sender, EventArgs e)
        {
            if (output != null)
            {
                try
                {
                    output.Close();
                }
                catch (IOException)
                {
                    MessageBox.Show("Error closing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            Application.Exit();
        }

        private void InputStudentInfoForm_Load(object sender, EventArgs e)
        {
            infoProcessLabel.Text = string.Empty;
            TextBoxesEnabled(false);
        }
    }
}
